package com.zhuoyue.web.controller.system;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.zhuoyue.system.domain.TblLocalSceneZigbeeDeviceRelation;
import com.zhuoyue.system.service.TblLocalSceneZigbeeDeviceRelationService;
import com.zhuoyue.web.controller.common.ControllerApiResult;
import com.zhuoyue.web.controller.system.util.ApiTokenUtil;
import com.zhuoyue.web.utils.OrgCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * (TblLocalSceneZigbeeDeviceRelation)表控制层
 *
 * @author hewenze
 * @since 2020-08-31 15:01:42
 */
@RestController
@RequestMapping("/system/scene/relation")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = "", description = "")
public class TblLocalSceneZigbeeDeviceRelationController extends ApiController {
    /**
     * 服务对象
     */
    private final TblLocalSceneZigbeeDeviceRelationService tblLocalSceneZigbeeDeviceRelationService;
    private final RedisTemplate redisTemplate;
    private final OrgCodeUtils orgCodeUtils;
    private final ApiTokenUtil apiTokenUtil;

    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
    @GetMapping("/sceneId")
    @ApiOperation(value = "查询", notes = "查询")
    public R selectAll(
            @RequestParam(value = "sceneId", required = false) String sceneId,
            HttpServletRequest request
    ) {

        String account = orgCodeUtils.getLoginUser(request).getUser().getPassword();
        String password = orgCodeUtils.getLoginUser(request).getUser().getPhonenumber();
        System.out.println(account);
        System.out.println(password);
        String orgCode = "zyzn";
        String type = "manage";
        String apiToken = apiTokenUtil.getApiToken(account, password, orgCode, type);


        List<TblLocalSceneZigbeeDeviceRelation> tblLocalSceneZigbeeDeviceRelations = this.tblLocalSceneZigbeeDeviceRelationService.selectByPrimaryKey(sceneId);


        for (TblLocalSceneZigbeeDeviceRelation tblLocalSceneZigbeeDeviceRelation : tblLocalSceneZigbeeDeviceRelations) {
            String cmd = tblLocalSceneZigbeeDeviceRelation.getCmd();

            Map<String, String> map = (Map) JSONObject.parse(tblLocalSceneZigbeeDeviceRelation.getControlParam());
            String gatewayMac = map.get("gatewayMac");

            String user_url = apiTokenUtil.getUrl() + "api/command/cmdSend?gatewayMac={gatewayMac}&cmd={cmd}";

            //String user_url = url;
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders requestHeaders = new HttpHeaders();
            //user_url = user_url + "account=" + account + "&password=" + password + "&type=" + "manage";
            requestHeaders.add("Authorization", apiToken);

            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("gatewayMac", gatewayMac);
            paramMap.put("cmd", cmd);


            org.springframework.http.HttpEntity<String> requestEntity = new org.springframework.http.HttpEntity<String>(null, requestHeaders);
            ResponseEntity<ControllerApiResult> response1 = restTemplate.exchange(user_url, HttpMethod.POST, requestEntity, ControllerApiResult.class, paramMap);
            //String tokenStr = response.getBody().getDataObject().toString();

            String msg = response1.getBody().getMsg();
            System.out.println("场景返回" + response1.getBody());
            if (!response1.getBody().isSuccess()) {
                return failed("执行失败");
            }

        }


        System.out.println(tblLocalSceneZigbeeDeviceRelations);


        return success("执行成功");
    }
    /*
     *//**
     * 通过scene_id查询数据
     *
     * @param scene_id 主键
     * @return 单条数据
     *//*
    @GetMapping("/scene_id")
    @ApiOperation(value = "通过scene_id查询数据", notes = "通过scene_id查询数据")
    public R selectOne(@RequestParam(value = "scene_id") String scene_id) {
        List<TblLocalSceneZigbeeDeviceRelation> tblLocalSceneZigbeeDeviceRelations = this.tblLocalSceneZigbeeDeviceRelationService.selectByPrimaryKey(scene_id);

        System.out.println(tblLocalSceneZigbeeDeviceRelations);
        return success(tblLocalSceneZigbeeDeviceRelations);
    }*/

    /*    *//**
     * 新增数据
     *
     * @param tblLocalSceneZigbeeDeviceRelation 实体对象
     * @return 新增结果
     *//*
    @PostMapping
    @ApiOperation(value = "添加", notes = "添加")
    public R insert(@RequestBody TblLocalSceneZigbeeDeviceRelation tblLocalSceneZigbeeDeviceRelation) {
        return success(this.tblLocalSceneZigbeeDeviceRelationService.save(tblLocalSceneZigbeeDeviceRelation));
    }


    *//**
     * 修改数据
     *
     * @param tblLocalSceneZigbeeDeviceRelation 实体对象
     * @return 修改结果
     *//*
    @PutMapping
    @ApiOperation(value = "修改", notes = "修改")
    public R update(@RequestBody TblLocalSceneZigbeeDeviceRelation tblLocalSceneZigbeeDeviceRelation) {
        return success(this.tblLocalSceneZigbeeDeviceRelationService.updateById(tblLocalSceneZigbeeDeviceRelation));
    }

    *//**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     *//*
    @DeleteMapping
    @ApiOperation(value = "删除", notes = "删除")
    public R delete(@RequestBody List
            <String> idList) {
        return success(this.tblLocalSceneZigbeeDeviceRelationService.removeByIds(idList));
    }*/
}
